package com.jinxin.platform.cddjnhjcxtsysset.rpc;

import com.alibaba.fastjson.JSON;
import com.jinxin.platform.base.api.annotation.APCOSApiOperation;
import com.jinxin.platform.base.api.annotation.NeedInf;
import com.jinxin.platform.base.api.constant.APCOSApiConstans;
import com.jinxin.platform.base.common.pojo.InvokeCfg;
import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.base.common.support.BusinessDelegate;
import com.jinxin.platform.cddjnhjcxtsysset.common.Constant;
import com.jinxin.platform.cddjnhjcxtsysset.common.SysOrgInfo;
import com.jinxin.platform.cddjnhjcxtsysset.common.SysUserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;

import static com.alibaba.fastjson.JSON.toJSONString;

/**
 * @description: (描述...)
 * @return:
 * @author: Linn
 * @time: 2020/05/18
 */
@Component
@Slf4j
@NeedInf(moduleKey = Constant.MODULE_KEY, className = Constant.MODULE_CLASS_NAME)
public class CddjnhjcxtsyssetRpc {
    @Autowired
    private BusinessDelegate businessDelegate;
    /**
     * 获取当前用户对象
     *
     * @return SysUser
     */
    @APCOSApiOperation(value = "getCurrentUser", description = "获取当前登录者", infDict = "获取当前登录者",
            reqDemo = "{}", respDemo = "{\"userId\":\"123\",\"userName\":\"张三\",\"userCellphoneNo\":\"13812345678\"}",
            infType = APCOSApiConstans.INF_TYPE_OTHER)
    public SysUserVO getCurrentUser() {
        JsonResult result = getJsonResult(Constant.MODULE_KEY,"getCurrentUser");
        if (result.getSuccess()) {
            if (result.getData() == null) {
                return null;
            }
            log.info("getCddjnhjcxtsyssetCurrentUser:" + result.getData());
            return JSON.parseObject(toJSONString(result.getData()), SysUserVO.class);
        }
//        else {
//            SysUserVO userVO = new SysUserVO();
//            userVO.setUserId("1");
//            userVO.setOrgId("1");
//            userVO.setUserAccount("admin");
//            userVO.setUserName("admin");
//            return userVO;
//        }
        return null;
    }

    /**
     * 获取当前用户id获取所在组织
     *
     * @return SysUser
     */
//    public SysOrgInfo getCurrentOrg() {
//        SysUserVO currentUser = getCurrentUser();
//        if(currentUser == null){return null; }
//        InvokeCfg cfg = new InvokeCfg();
//        cfg.setModuleKey(Constant.MODULE_KEY);
//        cfg.setClassName(Constant.MODULE_CLASS_NAME);
//        cfg.setFunName("getCurrentOrg");
//        HashMap<String, String> map = new HashMap<>();
//        map.put("userId",currentUser.getUserId());
//        cfg.setArgs(map);
//        JsonResult result = businessDelegate.invoke(cfg);
//        if (result.getSuccess()) {
//            if (result.getData() == null) {
//                return null;
//            }
//            log.info("getCddjnhjcxtsyssetOrg:" + result.getData());
//            return JSON.parseObject(toJSONString(result.getData()), SysOrgInfo.class);
//        }
//        else {
//            SysOrgInfo userVO = new SysOrgInfo();
//            userVO.setOrgId("1");
//            userVO.setOrgName("一个牛逼的公司");
//            return userVO;
//        }
//        return null;
//    }

    private JsonResult getJsonResult(String keyName,String funName) {
        InvokeCfg cfg = new InvokeCfg();
        cfg.setModuleKey(keyName);
        cfg.setClassName(Constant.MODULE_CLASS_NAME);
        cfg.setFunName(funName);
        return businessDelegate.invoke(cfg);
    }

}
